package me.where.ta;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import me.app.global.Constant;
import me.http.utils.MyHttpUtils;
import me.http.utils.MyThreadPool;
import me.where.home.R;

import org.json.JSONArray;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.widget.Toast;

import com.tencent.tencentmap.mapsdk.map.MapActivity;
import com.tencent.tencentmap.mapsdk.map.MapView;

public class TaMapActivity extends MapActivity {

	private MapView mapView;
	private ProgressDialog mProgressDialog;
	private int taId;
	private MyHandler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_fragment);
		this.mapView = (MapView) findViewById(R.id.mapview);

		this.mProgressDialog = new ProgressDialog(this);
		this.mProgressDialog.setMessage(Constant.DIALOG_MESSAGE);
		this.mProgressDialog.setCancelable(true);
		this.mProgressDialog.setCanceledOnTouchOutside(true);
		this.mProgressDialog.show();

		this.mHandler = new MyHandler(TaMapActivity.this);

		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		taId = bundle.getInt("id");

		if (taId != -1) {
			MyThreadPool.getInstance().getExecutorService()
					.submit(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							Map<String, String> params = new HashMap<String, String>();
							params.put("id", Integer.toString(taId));
							JSONArray taDatas = MyHttpUtils
									.getDatasByUrl(
											"http://192.168.9.37:8088/where/1/where/mysite/index.php/user/getta",
											params);
							if (taDatas != null) {
								mHandler.sendEmptyMessage(1);
							} else {
							}
						}
					});
		}

	}

	private static final class MyHandler extends Handler {
		private final WeakReference<TaMapActivity> taMapActivity;

		private MyHandler(TaMapActivity taMapActivity) {
			this.taMapActivity = new WeakReference<TaMapActivity>(taMapActivity);
		}

		@Override
		public void handleMessage(Message msg) {
			TaMapActivity taActivity = taMapActivity.get();
			if (taActivity != null) {
				switch (msg.what) {
				case 0:
					break;
				case 1:
					taActivity.dismissDialog();
					Toast.makeText(taActivity.getApplicationContext(),
							"获取Ta的数据成功！", Toast.LENGTH_SHORT).show();
					break;
				default:
					break;
				}
			}
		}
	};

	private void dismissDialog() {
		this.mProgressDialog.dismiss();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ta_map, menu);
		return true;
	}

}
